﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Entity;
using DAL;
using Common;

public partial class Customer_Document_Add : System.Web.UI.Page
{
    protected int ActiveTab
    {
        get { return string.IsNullOrEmpty(Request["ActiveTab"]) ? 0 : Convert.ToInt32(Request["ActiveTab"]); }
    }
    protected int DocKey,MUserKey;

    protected string MenuType
    {
        get { return string.IsNullOrEmpty(Request["menutype"]) ? "" : Request["menutype"].ToString(); }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        DocKey = string.IsNullOrEmpty(Request["DocKey"]) ? 0 : Convert.ToInt32(Request["DocKey"]);
        MUserKey = string.IsNullOrEmpty(Request["MUserKey"]) ? 0 : Convert.ToInt32(Request["MUserKey"]);

        if (!this.IsPostBack)
        {
            CustomerInfo.ActiveTabIndex = ActiveTab;

            if (DocKey != 0)
            {   
                 
                switch (CustomerInfo.ActiveTabIndex)
                {
                    case 0:
                        InfoServer server = new Document<InfoServer>().SelectDoc<InfoServer>(DocKey);
                        txt_ServerName1.Text = server.ServerName;
                        txt_MAC1.Text = server.MAC;
                        txt_IP1.Text = server.IP;
                        txt_UserName1.Text = server.UserName;
                        txt_Password1.Text = server.Password;
                        txt_Domain.Text = server.Domain;
                        txt_ManageInfo.Text = server.ManageInfo;
                        txt_OtherInfo.Text = server.OtherInfo;
                        Button1.Text = "修 改";
                        txt_MUserKey.Value = server.MUserKey.ToString();
                        break;
                    case 1:
                        InfoClient client = new Document<InfoClient>().SelectDoc<InfoClient>(DocKey);
                        txt_ClientName.Text = client.ClientName;
                        txt_MAC2.Text = client.MAC;
                        txt_IP2.Text = client.IP;
                        txt_WilessMAC.Text = client.WilessMAC;
                        txt_WirelessIP.Text = client.WirelessIP;
                        txt_UserName2.Text = client.UserName;
                        txt_Password2.Text = client.Password;
                        Button2.Text = "修 改";
                        txt_MUserKey.Value = client.MUserKey.ToString();
                        break;
                    case 2:
                        InfoDevice device = new Document<InfoDevice>().SelectDoc<InfoDevice>(DocKey);
                        txt_OutWeb.Text = device.OutWeb;
                        txt_ADSL.Text = device.ADSL;
                        txt_SwitchModel.Text = device.SwitchModel;
                        txt_RouterModel.Text = device.RouterModel;
                        txt_IP3.Text = device.IP;
                        txt_UserName3.Text = device.UserName;
                        txt_Password3.Text = device.Password;
                        txt_MAC3.Text = device.MAC;
                        txt_SSID3.Text = device.SSID;
                        txt_WirelessPwd.Text = device.WirelessPwd;
                        Button3.Text = "修 改";
                        txt_MUserKey.Value = device.MUserKey.ToString();
                        break;
                    case 3:
                        InfoOffice office = new Document<InfoOffice>().SelectDoc<InfoOffice>(DocKey);
                        txt_P_Model.Text = office.P_Model;
                        txt_P_IP.Text = office.P_IP;
                        txt_P_ShareName.Text = office.P_ShareName;
                        txt_P_ConnectPoint.Text = office.P_ConnectPoint;
                        txt_F_Model.Text = office.F_Model;
                        txt_F_Tel.Text = office.F_Tel;
                        txt_F_Ext.Text = office.F_Ext;
                        txt_O_Model.Text = office.O_Model;
                        txt_O_Remark.Text = office.O_Remark;
                        Button4.Text = "修 改";
                        txt_MUserKey.Value = office.MUserKey.ToString();
                        break;
                    case 4:
                        InfoSoft soft = new Document<InfoSoft>().SelectDoc<InfoSoft>(DocKey);
                        txt_SoftName.Text = soft.SoftName;
                        txt_ServerName5.Text = soft.ServerName;
                        txt_Admin.Text = soft.Admin;
                        txt_AdminPwd.Text = soft.AdminPwd;
                        txt_SoftList.Text = soft.SoftList;
                        Button5.Text = "修 改";
                        txt_MUserKey.Value = soft.MUserKey.ToString();
                        break;
                }
                txt_CustName.Text = new Account().GetAccountCompanyName(Convert.ToInt32(txt_MUserKey.Value));
            }
       }
    }

    //添加
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (DocKey != 0)
        {
            switch (CustomerInfo.ActiveTabIndex)
            {
                case 0:
                    InfoServer infoserver = new InfoServer
                    {   
                        ServerKey = DocKey,
                        MUserKey = Convert.ToInt32(txt_MUserKey.Value),
                        ServerName = ((TextBox)TabPanel1.FindControl("txt_ServerName1")).Text.Trim(),
                        MAC = ((TextBox)TabPanel1.FindControl("txt_MAC1")).Text.Trim(),
                        IP = ((TextBox)TabPanel1.FindControl("txt_IP1")).Text.Trim(),
                        UserName = ((TextBox)TabPanel1.FindControl("txt_UserName1")).Text.Trim(),
                        Password = ((TextBox)TabPanel1.FindControl("txt_Password1")).Text.Trim(),
                        Domain = ((TextBox)TabPanel1.FindControl("txt_Domain")).Text.Trim(),
                        ManageInfo = ((TextBox)TabPanel1.FindControl("txt_ManageInfo")).Text.Trim(),
                        OtherInfo = ((TextBox)TabPanel1.FindControl("txt_OtherInfo")).Text.Trim()
                    };
                    EditDoc<InfoServer>(infoserver);
                    break;
                case 1:
                    InfoClient infoclient = new InfoClient
                    {    
                        ClientKey = DocKey,
                        MUserKey = Convert.ToInt32(txt_MUserKey.Value),
                        ClientName = ((TextBox)TabPanel2.FindControl("txt_ClientName")).Text.Trim(),
                        MAC = ((TextBox)TabPanel2.FindControl("txt_MAC2")).Text.Trim(),
                        IP = ((TextBox)TabPanel2.FindControl("txt_IP2")).Text.Trim(),
                        WilessMAC = ((TextBox)TabPanel2.FindControl("txt_WilessMAC")).Text.Trim(),
                        WirelessIP = ((TextBox)TabPanel2.FindControl("txt_WirelessIP")).Text.Trim(),
                        UserName = ((TextBox)TabPanel2.FindControl("txt_UserName2")).Text.Trim(),
                        Password = ((TextBox)TabPanel2.FindControl("txt_Password2")).Text.Trim()
                    };
                    EditDoc<InfoClient>(infoclient);
                    break;
                case 2:
                    InfoDevice infodevice = new InfoDevice
                    {   
                        DeviceKey = DocKey,
                        MUserKey = Convert.ToInt32(txt_MUserKey.Value),
                        OutWeb = ((TextBox)TabPanel3.FindControl("txt_OutWeb")).Text.Trim(),
                        ADSL = ((TextBox)TabPanel3.FindControl("txt_ADSL")).Text.Trim(),
                        SwitchModel = ((TextBox)TabPanel3.FindControl("txt_SwitchModel")).Text.Trim(),
                        RouterModel = ((TextBox)TabPanel3.FindControl("txt_RouterModel")).Text.Trim(),
                        IP = ((TextBox)TabPanel3.FindControl("txt_IP3")).Text.Trim(),
                        UserName = ((TextBox)TabPanel3.FindControl("txt_UserName3")).Text.Trim(),
                        Password = ((TextBox)TabPanel3.FindControl("txt_Password3")).Text.Trim(),
                        MAC = ((TextBox)TabPanel3.FindControl("txt_MAC3")).Text.Trim(),
                        SSID = ((TextBox)TabPanel3.FindControl("txt_SSID3")).Text.Trim(),
                        WirelessPwd = ((TextBox)TabPanel3.FindControl("txt_WirelessPwd")).Text.Trim()
                    };
                    EditDoc<InfoDevice>(infodevice);
                    break;
                case 3:
                    InfoOffice infooffice = new InfoOffice
                    {   
                        OfficeKey = DocKey,
                        MUserKey = Convert.ToInt32(txt_MUserKey.Value),
                        P_Model = ((TextBox)TabPanel4.FindControl("txt_P_Model")).Text.Trim(),
                        P_IP = ((TextBox)TabPanel4.FindControl("txt_P_IP")).Text.Trim(),
                        P_ShareName = ((TextBox)TabPanel4.FindControl("txt_P_ShareName")).Text.Trim(),
                        P_ConnectPoint = ((TextBox)TabPanel4.FindControl("txt_P_ConnectPoint")).Text.Trim(),
                        F_Model = ((TextBox)TabPanel4.FindControl("txt_F_Model")).Text.Trim(),
                        F_Tel = ((TextBox)TabPanel4.FindControl("txt_F_Tel")).Text.Trim(),
                        F_Ext = ((TextBox)TabPanel4.FindControl("txt_F_Ext")).Text.Trim(),
                        O_Model = ((TextBox)TabPanel4.FindControl("txt_O_Model")).Text.Trim(),
                        O_Remark = ((TextBox)TabPanel4.FindControl("txt_O_Remark")).Text.Trim(),
                    };
                    EditDoc<InfoOffice>(infooffice);
                    break;
                case 4:
                    InfoSoft infosoft = new InfoSoft
                    {   
                        SoftKey = DocKey,
                        MUserKey = Convert.ToInt32(txt_MUserKey.Value),
                        SoftName = ((TextBox)TabPanel5.FindControl("txt_SoftName")).Text.Trim(),
                        ServerName = ((TextBox)TabPanel5.FindControl("txt_ServerName5")).Text.Trim(),
                        Admin = ((TextBox)TabPanel5.FindControl("txt_Admin")).Text.Trim(),
                        AdminPwd = ((TextBox)TabPanel5.FindControl("txt_AdminPwd")).Text.Trim(),
                        SoftList = ((TextBox)TabPanel5.FindControl("txt_SoftList")).Text.Trim()
                    };
                    EditDoc<InfoSoft>(infosoft);
                    break;
            }
        }
        else
        {
            switch (CustomerInfo.ActiveTabIndex)
            {
                case 0:
                    InfoServer infoserver = new InfoServer
                    {
                        MUserKey = Convert.ToInt32(txt_MUserKey.Value),
                        ServerName = ((TextBox)TabPanel1.FindControl("txt_ServerName1")).Text.Trim(),
                        MAC = ((TextBox)TabPanel1.FindControl("txt_MAC1")).Text.Trim(),
                        IP = ((TextBox)TabPanel1.FindControl("txt_IP1")).Text.Trim(),
                        UserName = ((TextBox)TabPanel1.FindControl("txt_UserName1")).Text.Trim(),
                        Password = ((TextBox)TabPanel1.FindControl("txt_Password1")).Text.Trim(),
                        Domain = ((TextBox)TabPanel1.FindControl("txt_Domain")).Text.Trim(),
                        ManageInfo = ((TextBox)TabPanel1.FindControl("txt_ManageInfo")).Text.Trim(),
                        OtherInfo = ((TextBox)TabPanel1.FindControl("txt_OtherInfo")).Text.Trim(),
                        CreateTime = DateTime.Now,
                        UserKey = Convert.ToInt32(Session["UserKey"])
                    };
                    AddDoc<InfoServer>(infoserver);
                    break;
                case 1:
                    InfoClient infoclient = new InfoClient
                    {
                        MUserKey = Convert.ToInt32(txt_MUserKey.Value),
                        ClientName = ((TextBox)TabPanel2.FindControl("txt_ClientName")).Text.Trim(),
                        MAC = ((TextBox)TabPanel2.FindControl("txt_MAC2")).Text.Trim(),
                        IP = ((TextBox)TabPanel2.FindControl("txt_IP2")).Text.Trim(),
                        WilessMAC = ((TextBox)TabPanel2.FindControl("txt_WilessMAC")).Text.Trim(),
                        WirelessIP = ((TextBox)TabPanel2.FindControl("txt_WirelessIP")).Text.Trim(),
                        UserName = ((TextBox)TabPanel2.FindControl("txt_UserName2")).Text.Trim(),
                        Password = ((TextBox)TabPanel2.FindControl("txt_Password2")).Text.Trim(),
                        CreateTime = DateTime.Now,
                        UserKey = Convert.ToInt32(Session["UserKey"])
                    };
                    AddDoc<InfoClient>(infoclient);
                    break;
                case 2:
                    InfoDevice infodevice = new InfoDevice
                    { 
                        MUserKey = Convert.ToInt32(txt_MUserKey.Value),
                        OutWeb = ((TextBox)TabPanel3.FindControl("txt_OutWeb")).Text.Trim(),
                        ADSL = ((TextBox)TabPanel3.FindControl("txt_ADSL")).Text.Trim(),
                        SwitchModel = ((TextBox)TabPanel3.FindControl("txt_SwitchModel")).Text.Trim(),
                        RouterModel = ((TextBox)TabPanel3.FindControl("txt_RouterModel")).Text.Trim(),
                        IP = ((TextBox)TabPanel3.FindControl("txt_IP3")).Text.Trim(),
                        UserName = ((TextBox)TabPanel3.FindControl("txt_UserName3")).Text.Trim(),
                        Password = ((TextBox)TabPanel3.FindControl("txt_Password3")).Text.Trim(),
                        MAC = ((TextBox)TabPanel3.FindControl("txt_MAC3")).Text.Trim(),
                        SSID = ((TextBox)TabPanel3.FindControl("txt_SSID3")).Text.Trim(),
                        WirelessPwd = ((TextBox)TabPanel3.FindControl("txt_WirelessPwd")).Text.Trim(),
                        CreateTime = DateTime.Now,
                        UserKey = Convert.ToInt32(Session["UserKey"])
                    };
                    AddDoc<InfoDevice>(infodevice);
                    break;
                case 3:
                    InfoOffice infooffice = new InfoOffice
                    {
                        MUserKey = Convert.ToInt32(txt_MUserKey.Value),
                        P_Model = ((TextBox)TabPanel4.FindControl("txt_P_Model")).Text.Trim(),
                        P_IP = ((TextBox)TabPanel4.FindControl("txt_P_IP")).Text.Trim(),
                        P_ShareName = ((TextBox)TabPanel4.FindControl("txt_P_ShareName")).Text.Trim(),
                        P_ConnectPoint = ((TextBox)TabPanel4.FindControl("txt_P_ConnectPoint")).Text.Trim(),
                        F_Model = ((TextBox)TabPanel4.FindControl("txt_F_Model")).Text.Trim(),
                        F_Tel = ((TextBox)TabPanel4.FindControl("txt_F_Tel")).Text.Trim(),
                        F_Ext = ((TextBox)TabPanel4.FindControl("txt_F_Ext")).Text.Trim(),
                        O_Model = ((TextBox)TabPanel4.FindControl("txt_O_Model")).Text.Trim(),
                        O_Remark = ((TextBox)TabPanel4.FindControl("txt_O_Remark")).Text.Trim(),
                        CreateTime = DateTime.Now,
                        UserKey = Convert.ToInt32(Session["UserKey"])
                    };
                    AddDoc<InfoOffice>(infooffice);
                    break;
                case 4:
                    InfoSoft infosoft = new InfoSoft
                    { 
                        MUserKey = Convert.ToInt32(txt_MUserKey.Value),
                        SoftName = ((TextBox)TabPanel5.FindControl("txt_SoftName")).Text.Trim(),
                        ServerName = ((TextBox)TabPanel5.FindControl("txt_ServerName5")).Text.Trim(),
                        Admin = ((TextBox)TabPanel5.FindControl("txt_Admin")).Text.Trim(),
                        AdminPwd = ((TextBox)TabPanel5.FindControl("txt_AdminPwd")).Text.Trim(),
                        SoftList = ((TextBox)TabPanel5.FindControl("txt_SoftList")).Text.Trim(),
                        CreateTime = DateTime.Now,
                        UserKey = Convert.ToInt32(Session["UserKey"])
                    };
                    AddDoc<InfoSoft>(infosoft);
                    break;
            }
        }
       
    }

    private void AddDoc<T>(T DocObj)
    {

        if (new Document<T>().AddDoc(DocObj))
        {
            WebUtility.JsAlertToGo("添加客户资料成功!", "Document_Add.aspx?menutype=411&activetab=" + CustomerInfo.ActiveTabIndex);
        }
        else
        {

        }

    }

    private void EditDoc<T>(T DocObj)
    {

        if (new Document<T>().EditDoc(DocObj))
        {
            WebUtility.JsAlertToGo("修改客户资料成功!", "Document.aspx?menutype=411");
        }
        else
        {

        }
    }

}
